﻿Imports Microsoft.VisualBasic
Imports System.Data

<System.ComponentModel.DataObjectAttribute(True)> _
Public Class BLL_AddTrainingCourseGroup

    Public Sub New()
        ' 
        ' TODO: 在此加入建構函式的程式碼 
        ' 
    End Sub

    'SELECT 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.[Select])> _
    Public Function BLL_Select(ByVal Id As Integer) As DataTable

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("SELECT * ")
        sql.AppendFormat("FROM TrainingCourseGroup ")
        sql.AppendFormat("WHERE id={0}", Id)
        sql.AppendFormat("order by StartDate desc ")
        Return dat.GetData(sql)

    End Function

    'Insert
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert)> _
    Public Function BLL_Insert(ByVal GroupName As String, ByVal GroupType As String, ByVal AllHour As Integer, ByVal PersonLimit As Integer, ByVal Candidate As Integer, ByVal ClassPlace As String, ByVal Org As String, ByVal StartDate As DateTime, ByVal EndDate As DateTime, ByVal RegStartDate As DateTime, ByVal RegEndDate As DateTime, ByVal UOrganizationID As String, ByVal LoginUser As String) As Integer
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.Length = 0

            sql.AppendFormat("insert TrainingCourseGroup (GroupName,GroupType,AllHour,PersonLimit,Candidate,ClassPlace,Org,StartDate,EndDate,RegStartDate,RegEndDate,UOrganizationID) select ")
            sql.AppendFormat(" '{0}' ", GroupName)
            sql.AppendFormat(",'{0}' ", GroupType)
            sql.AppendFormat(",{0} ", AllHour)
            sql.AppendFormat(",{0} ", PersonLimit)
            sql.AppendFormat(",{0} ", Candidate)
            sql.AppendFormat(",'{0}' ", ClassPlace)
            sql.AppendFormat(",'{0}' ", Org)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", StartDate)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", EndDate)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", RegStartDate)
            sql.AppendFormat(",'{0:yyyy/MM/dd}' ", RegEndDate)
            sql.AppendFormat(",'{0}'; ", UOrganizationID)

            sql.AppendFormat("UPDATE TrainingCourseGroupMapping SET GroupId=@@IDENTITY  WHERE GroupId=0 and CreateBy='{0}';", LoginUser)
            dat.AccData(sql)

            scope.Complete()
            '
            Return 1

        End Using

    End Function

    'Update 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)> _
       Public Function BLL_Update(ByVal GroupName As String, ByVal GroupType As String, ByVal AllHour As Integer, ByVal PersonLimit As Integer, ByVal Candidate As Integer, ByVal ClassPlace As String, ByVal Org As String, ByVal StartDate As DateTime, ByVal EndDate As DateTime, ByVal RegStartDate As DateTime, ByVal RegEndDate As DateTime, ByVal UOrganizationID As String, ByVal original_Id As Integer) As Integer
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.AppendFormat("UPDATE TrainingCourseGroup ")
            sql.AppendFormat("SET GroupName='{0}' ", GroupName)
            sql.AppendFormat(",GroupType='{0}'", GroupType)
            sql.AppendFormat(",AllHour={0}", AllHour)
            sql.AppendFormat(",PersonLimit={0}", PersonLimit)
            sql.AppendFormat(",Candidate={0}", Candidate)
            sql.AppendFormat(",ClassPlace='{0}'", ClassPlace)
            sql.AppendFormat(",Org='{0}'", Org)
            sql.AppendFormat(",StartDate='{0:yyyy/MM/dd}'", StartDate)
            sql.AppendFormat(",EndDate='{0:yyyy/MM/dd}'", EndDate)
            sql.AppendFormat(",RegStartDate='{0:yyyy/MM/dd}'", RegStartDate)
            sql.AppendFormat(",RegEndDate='{0:yyyy/MM/dd}'", RegEndDate)
            sql.AppendFormat(",UOrganizationID='{0}'", UOrganizationID)

            sql.AppendFormat("WHERE Id={0}; ", original_Id)
            'MyObj.WriteLog(sql.ToString())
            dat.AccData(sql)
            scope.Complete()

            Return 1

        End Using

    End Function

End Class
